package com.barnett.andbase;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.concurrent.ScheduledThreadPoolExecutor;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.barnett.utils.DensityUtils;
import com.barnett.view.DrawableTool;

/**
 * TODO<基础的Activity>
 * 
 * @author JiangJian
 * @version: V1.0
 */
public abstract class BaseActivity extends Activity implements OnClickListener {

	private Toast mToast;
	private ProgressDialog progressDialog;
	public static final int TIMEOUT = -1;
	public static final int ERROR = -2;
	public static final int NONET = -3;
	public static final int DATAERROR = -4;
	/** 代替Timer的定时器 */
	protected ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(5);
		int contentRes = setContentView();
		if (contentRes != 0) {
			setContentView(contentRes);
		}
		try {
			autoInjectAllField(this);
		} catch (IllegalAccessException e) {
		} catch (IllegalArgumentException e) {
		}
		this.registerListener();
		this.initData();
	}

	/** 展示圆进度条 */
	@SuppressLint("NewApi")
	protected void showProgressDialog(String msg) {
		// TODO Auto-generated method stub
		if (progressDialog == null) {
			progressDialog = new ProgressDialog(this);
			progressDialog.setCanceledOnTouchOutside(false);
		}
		progressDialog.show();

		RelativeLayout relativeLayout = new RelativeLayout(this);
		relativeLayout.setGravity(Gravity.CENTER);

		LinearLayout layout = new LinearLayout(this);
		layout.setGravity(Gravity.CENTER_HORIZONTAL);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setBackground(DrawableTool.getShapeDrawable(1, 15, Color.WHITE,
				Color.WHITE));
		layout.setPadding(20, 50, 20, 50);

		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				DensityUtils.dp2px(this, 200), LayoutParams.WRAP_CONTENT);
		ProgressBar progressBar = new ProgressBar(this);

		TextView textView = new TextView(this);
		textView.setTextColor(Color.BLACK);
		textView.setTextSize(20);
		textView.setGravity(Gravity.CENTER);
		LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

		layout.addView(progressBar);
		layout.addView(textView, textParams);

		relativeLayout.addView(layout, layoutParams);

		progressDialog.getWindow().setContentView(relativeLayout);
		textView.setText(TextUtils.isEmpty(msg) ? "" : msg);
	}

	/** 隐藏圆进度条 */
	protected void dismissProgressDialog() {
		// TODO Auto-generated method stub
		if (progressDialog != null) {
			try {
				progressDialog.dismiss();
			} catch (IllegalArgumentException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}

	/** Toast提示 */
	protected void showToast(String text) {
		if (mToast == null) {
			mToast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
		} else {
			mToast.setText(text);
		}
		mToast.show();
	}

	/** 中间位置Toast提示 */
	protected void showCenterToast(final String text) {
		if (mToast == null) {
			mToast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
			mToast.setGravity(Gravity.CENTER, 0, 0);
			mToast.show();
		} else {
			mToast.setText(text);
		}
		mToast.show();
	}

	/** 加载控件 */
	protected abstract int setContentView();

	/** 加载控件数据 */
	protected abstract void initData();

	/** 控件的点击事件 */
	protected abstract void registerListener();

	/** 控件的监听方法 */
	@Override
	public abstract void onClick(View v);

	/**
	 * Android中通过注解代替findViewById方法
	 * 
	 * @param activity
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	private void autoInjectAllField(Activity activity)
			throws IllegalAccessException, IllegalArgumentException {
		// 得到Activity对应的Class
		Class<? extends BaseActivity> clazz = this.getClass();
		// 得到该Activity的所有字段
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			// 判断字段是否标注InjectView
			if (field.isAnnotationPresent(FindView.class)) {
				// 如果标注了，就获得它的id
				FindView inject = field.getAnnotation(FindView.class);
				int id = inject.id();
				if (id > 0) {
					// 反射访问私有成员，必须加上这句
					field.setAccessible(true);
					// 然后对这个属性复制
					field.set(activity, activity.findViewById(id));
				}
			}
		}
	}

	@Target(ElementType.FIELD)
	@Retention(RetentionPolicy.RUNTIME)
	public @interface FindView {
		// id就是控件id，在某一个控件上使用注解标注其id
		int id() default -1;
	}
}